<?php

/*
 * File containing all the commonly used generic functions
 * ===============================================
 * Date: March 17th, 2012
 * Created By: Integration Team
 * ===============================================
 */

function cleanup($string){
    $cleanString = str_replace(array("\r\n", "\r", "\n", "\t"), '', $string);
    $cleanString = str_replace("\"", "\"\"", $cleanString);
    return $cleanString;
}


function truncate_words($string = "", $maxCharacters = 0) 
{
//Function for truncating to the closest word 
//in order to avoid partial words 
    $new_string = "";
    if ($string <> "" && $maxCharacters > 0) 
      {
        $words = explode(" ", $string);
            foreach ($words as $word) 
            {
                if (strlen($new_string) + strlen($word) < $maxCharacters) 
                {
                    $new_string.=" " . $word;
                }
            }
        return $new_string;
    } 
    else 
    {
        return false;
    }
}

function URL_key($string)
{
  //Prepares a string to be used as a URL Key
  //This would strip out any trailing spaces,
  //\r, \n, \t, --, or anything that is not
  //Alpha-numeric or a hyphen
    
 $string = strtolower(trim($string));
 $string = preg_replace('/\s\s+/',' ',$string);
 $string = str_replace(" ","-", $string);
 $string = preg_replace('/-+/','-',$string);
 $string = preg_replace('/[^A-Za-z0-9\-]/','',$string);
 $string = trim($string,'-');
 return $string;
}

//test

?>
